package com.wenhua.junshao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenhua.junshao.common.BaseContext;
import com.wenhua.junshao.common.R;
import com.wenhua.junshao.entity.Orders;
import com.wenhua.junshao.service.OrderDetailService;
import com.wenhua.junshao.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     */
    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据:{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }


    /**
     * 下单之后查看订单
     */
    @GetMapping("userPage")
    public R<Page<Orders>> userPage(long page, long pageSize) {
        //创建分页构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        //添加条件
        LambdaQueryWrapper<Orders> eq = Wrappers.lambdaQuery(Orders.class)
                .eq(BaseContext.getCurrentId() != null, Orders::getUserId, BaseContext.getCurrentId());
        eq.orderByDesc(Orders::getOrderTime);

        orderService.page(pageInfo, eq);
        return R.success(pageInfo);

    }

    /**
     * 后台系统查看订单明细
     */
    @GetMapping("page")
    public R<Page<Orders>> page(long page, long pageSize) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.lambdaQuery(Orders.class);
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }
}
